/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Modulo4;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMessage;

import Modelo.Asistencia;
import Modelo.Suplencia;
import Modelo.DAL.Suplencia_DAO;
import Modelo.DAL.Guardia_DAO;
import Acceso.Security;
/**
 *
 * @author yngrdyn
 */
public class agregarCambioGuardiaAction extends org.apache.struts.action.Action {
    
    /* forward name="success" path="" */
    private static final String SUCCESS = "success";
    private static final String FAILURE = "failure";
    
    /**
     * This is the action called from the Struts framework.
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        String[] a = {"Administrador","Director DSI","Jefe Apoyo Administrativo"};
       if (!Security.validateSession(request, a)) {
            return mapping.findForward("errorPriv");
        }
        System.out.println("Paso Autenticación");

        agregarCambioGuardiaForm formBean = (agregarCambioGuardiaForm)form;

        String fechaG = formBean.getFechaG();
        String sede = formBean.getSede();
        String ci = formBean.getCi();
        String reemplazo = formBean.getCi_reemplazo();
        String autorizado = formBean.getAutorizado();
        String voe = formBean.getVoe();

        String citotal = voe+"-"+reemplazo;
        ActionErrors errors = new ActionErrors();

        Guardia_DAO guardD = new Guardia_DAO();
        Long idGuardLong = guardD.obtenerIdGuardia(fechaG, ci);
        if(idGuardLong == null){
            errors.add("cambio_fallido", new ActionMessage("errors.cambio_fallido"));
            saveErrors(request, errors);
            return mapping.findForward(FAILURE);
        }
        formBean.setIdGuardia(idGuardLong);
if(autorizado.equals("seleccionar"))
        {
            errors.add("autorizacion_requerida", new ActionMessage("errors.autorizacion_requerida"));
            saveErrors(request, errors);

            return mapping.findForward(FAILURE);
        }

        if(reemplazo.equals(""))
        {
            Suplencia ast = new Suplencia(ci,"",idGuardLong,autorizado);

            Suplencia_DAO supD = new Suplencia_DAO();

            boolean verif = supD.insertarSuplencia(ast);
            if(!verif)
            {
             errors.add("cambio_noEfectuado", new ActionMessage("errors.cambio_noEfectuado"));
             saveErrors(request, errors);
             return mapping.findForward(FAILURE);
            }
            
            //verif = supD.marcarCambioGuardiaAsistencia(idGuardLong, ci,"");
           if(!verif)
           {
             errors.add("cambio_noEfectuado", new ActionMessage("errors.cambio_noEfectuado"));
             saveErrors(request, errors);
             return mapping.findForward(FAILURE);
           }

            errors.add("cambio_exitoso", new ActionMessage("errors.cambio_exitoso"));
            saveErrors(request, errors);
            return mapping.findForward(SUCCESS);
        }

        
        
        Suplencia ast = new Suplencia(ci,citotal,idGuardLong,autorizado);
        
        Suplencia_DAO supD = new Suplencia_DAO();

        boolean verif = supD.insertarSuplencia(ast);
        if(!verif)
        {
            errors.add("cambio_noEfectuado", new ActionMessage("errors.cambio_noEfectuado"));
            saveErrors(request, errors);
            return mapping.findForward(FAILURE);
        }
        verif = supD.marcarCambioGuardiaAsistencia(idGuardLong, ci,citotal);
        System.out.println(verif);
        if(!verif)
        {
            errors.add("cambio_noEfectuado1", new ActionMessage("errors.cambio_noEfectuado1"));
            saveErrors(request, errors);
            return mapping.findForward(FAILURE);
        }

        errors.add("cambio_exitoso", new ActionMessage("errors.cambio_exitoso"));
        saveErrors(request, errors);
        return mapping.findForward(SUCCESS);
    }
}
